/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.operation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.operation.dto.RecoveryParam;
import com.nai365.nart.operation.entity.RecoveryRecord;
import com.nai365.nart.operation.service.RecoveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 回收功能控制器
 *
 * @author linco
 * @version 1.0
 * @date 2022-07-22 13:29:36
 * @since 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/recovery")
@Api(value = "recovery", tags = "回收管理")
public class RecoveryController {

	private final RecoveryService recoveryRecordService;

	/**
	 * 回收记录分页查询
	 *
	 * @param page           分页对象
	 * @param recoveryRecord 回收记录分页对象
	 * @return 回收记录分页对象
	 */
	@ApiOperation(value = "回收记录分页查询", tags = {"回收管理-PC端"})
	@GetMapping("/record/page")
	public R<Page<RecoveryRecord>> getRecordPage(Page<RecoveryRecord> page, RecoveryRecord recoveryRecord) {
		return R.ok(recoveryRecordService.page(page, Wrappers.query(recoveryRecord)));
	}

	/**
	 * 我的回收记录
	 *
	 * @param page 分页对象
	 * @return 回收记录分页对象
	 */
	@ApiOperation(value = "我的回收记录分页查询", tags = {"回收管理-移动端"})
	@GetMapping("/my/record/page")
	public R<Page<RecoveryRecord>> myRecord(Page<RecoveryRecord> page) {
		LambdaQueryWrapper<RecoveryRecord> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(RecoveryRecord::getUserId, SecurityUtils.getUser().getId());
		return R.ok(recoveryRecordService.page(page, queryWrapper));
	}


	/**
	 * 回收记录详情
	 *
	 * @param id id
	 * @return 回收记录详情
	 */
	@ApiOperation(value = "回收记录详情", tags = {"回收管理-PC端", "回收管理-移动端"})
	@GetMapping("/record/{id}")
	public R<RecoveryRecord> getById(@PathVariable("id") @ApiParam("回收记录id") Long id) {
		return R.ok(recoveryRecordService.getById(id));
	}

	/**
	 * 回收
	 *
	 * @param recoveryParam 回收参数
	 * @return R
	 */
	@ApiOperation(value = "回收", tags = "回收管理-移动端")
	@SysLog("回收")
	@PostMapping
	public R<RecoveryRecord> recovery(@RequestBody RecoveryParam recoveryParam) {
		try {
			RecoveryRecord recoveryRecord = recoveryRecordService.recovery(recoveryParam);
			recoveryRecordService.recoveryChain(recoveryRecord.getId());
			return R.ok(recoveryRecord, "回收成功,等待上链处理!");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}


}
